package dyyx.drools;

import java.io.StringReader;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.definition.KnowledgePackage;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatelessKnowledgeSession;



public class DroolsHello{
	

	
	
    public static void main(String[] args) throws Exception{  	
    	Map map = new LinkedHashMap();
    	compileAndRunTest(map);
    	System.out.println(map);
    	
    	
 		
    	
    }
    
    
  
    
    
    public static void compileAndRunTest(Map resultMap){
    	
    	String drl = getDrl();
 		long start0 = System.currentTimeMillis();
 		long start = System.currentTimeMillis();
 		KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
 		// org.drools.impl.KnowledgeBuilderImpl
 		System.out.println("kbuilder="+kbuilder);
 		long end = System.currentTimeMillis();
 		long time = end - start;
 		resultMap.put("compileTime1", time);

 		start = System.currentTimeMillis();
 		kbuilder.add(ResourceFactory.newReaderResource(new StringReader(drl)), ResourceType.DRL);
 		end = System.currentTimeMillis();
 		time = end - start;
 		resultMap.put("compileTime2", time);
 		
 		start = System.currentTimeMillis();
 		Collection<KnowledgePackage> pkgs = kbuilder.getKnowledgePackages();
 		end = System.currentTimeMillis();
 		time = end - start;
 		resultMap.put("compileTime3", time);
 		
 		start = System.currentTimeMillis();
 		KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
 		end = System.currentTimeMillis();
 		time = end - start;
 		resultMap.put("compileTime4", time);
 		
 		start = System.currentTimeMillis();
 		kbase.addKnowledgePackages(pkgs);
 		end = System.currentTimeMillis();
 		time = end - start;
 		resultMap.put("compileTime5", time);

 		// System.out.println("time="+time);
 		time = end - start0;
 		resultMap.put("compileTimeAll", time);
 		
 		start = System.currentTimeMillis();
 		StatelessKnowledgeSession session = kbase.newStatelessKnowledgeSession();
 		end = System.currentTimeMillis();
 		time = end - start;
 		resultMap.put("createSessionTime", time);

 		// ScoreHandler scoreHandler = new ScoreHandler();
 		// session.setGlobal("score_handler", scoreHandler);

 		Map context = new HashMap();
 		context.put("age",20);

 		start = System.currentTimeMillis();
 		session.execute(context);
 		end = System.currentTimeMillis();
 		time = end - start;
 		resultMap.put("runTime1", time);
 		
 		resultMap.put("result1", context+"");

 		// System.out.println(context);

 		context = new HashMap();
 		context.put("age",9);
 		
 		start = System.currentTimeMillis();
 		session.execute(context);
 		end = System.currentTimeMillis();
 		time = end - start;
 		resultMap.put("runTime2", time);
 		
 		resultMap.put("result2", context+"");

 		
 		// System.out.println(context);
 		
 	
    }

	
	
	public static String getDrl(){
		StringBuilder sb = new StringBuilder();
		
		sb.append("package com.dyyx.drools.test \n");
		sb.append("import java.util.Map   \n");
		sb.append("rule \"simple rule 001\"   \n");
		sb.append("salience 1 \n");
		sb.append("no-loop true   \n");
		sb.append("when \n");
		sb.append("context: Map(age > 18)   \n");
		sb.append("then \n");
		sb.append("context.put(\"match\",true); \n");
		sb.append("end \n");
		
		
		return sb.toString();
	}


}
